package com.pda.gongpin.view;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;


import com.google.gson.Gson;
import com.pda.gongpin.bean.ScanBean;


public class ScanButView extends androidx.appcompat.widget.AppCompatButton implements View.OnLongClickListener {

    private Vibrator mVibrator;

    private SoundPool soundpool = null;
    private int soundid;
    private String barcodeStr;
    private boolean isScaning = false;
    private String mBroadcastAction ="android.intent.ACTION_SCAN_OUTPUT";
    private String mBroadcastLabel = "barcode";
    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(mBroadcastAction)) {
                barcodeStr = intent.getStringExtra(mBroadcastLabel);
                // TODO Auto-generated method stub
                isScaning = false;
                ScanBean scanBean = null;
                try {
                    scanBean = new Gson().fromJson(barcodeStr, ScanBean.class);
                    if (scanBean.getType() == 0){
                        scanBean=new ScanBean(barcodeStr,0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //Toast.makeText(context, "请扫正确的二维码", Toast.LENGTH_SHORT).show();
                    //return;
                    scanBean=new ScanBean(barcodeStr,-1);
                }
                //showScanResult.setText(barcodeStr);
                if (scanCallBack != null) {
                    scanCallBack.callBack(scanBean);
                }
            }
        }

    };


    public ScanButView(Context context) {
        this(context,null);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnLongClickListener(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(mBroadcastAction);
        getContext().registerReceiver(mScanReceiver, filter);
    }



    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isScaning = false;
        try {
            getContext().unregisterReceiver(mScanReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            stopScan();
        } else if (event.getAction() == MotionEvent.ACTION_DOWN){
            startScan();
        }
        return super.onTouchEvent(event);
    }

    private void stopScan() {
        Intent intent = new Intent();
        intent.setAction("android.intent.ACTION_SCAN_STOP");
        getContext().sendBroadcast(intent);
    }

    @Override
    public boolean onLongClick(View v) {
        //startScan();
        return false;
    }

    private void startScan() {
        isScaning = true;
        Intent intent = new Intent();
        intent.setAction("android.intent.ACTION_SCAN_START");
        getContext().sendBroadcast(intent);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void setScanCallBack(ScanCallBack scanCallBack) {
        this.scanCallBack = scanCallBack;
    }

    private ScanCallBack scanCallBack ;



    public interface ScanCallBack{
        void callBack(ScanBean scanBean);
    }
}
